/*
	jME Terra package. A Terrain Engine for the Java Monkey Engine.
	Copyright (C) 2005-2006,  Tijl Houtbeckers
	 
	**** LICENSE: ****

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation,
	version 2.1.
	
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

	*** CONTACT: ****
	
	I can be reached through the jmonkeyengine.com forums (user "llama"), 
	or the jmonkeyengine.com contact page:
	jmonkeyengine.com/index.php?option=com_contact&task=view&contact_id=5           
	
*/

/**
 * @author Tijl Houtbeckers
 */

package org.llama.jmex.terra.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public abstract class TerraBuilder {

	public static String readString(String text, String value) throws IOException {
		if (value != null) {
			System.out.println(text + ": " + value);
			return value;
		}
		System.out.print(text + ": ");
		return (new BufferedReader(new InputStreamReader(System.in))).readLine();
	}

	public static int readInt(String text, int i) throws NumberFormatException, IOException {
		if (i != -1) {
			System.out.println(text + ": " + i);
			return i;
		}
		return Integer.parseInt(readString(text, null));
	}

	public void build(String arg[]) {
		try {
			String prefix_in = null;
			String prefix_out = null;
			int mapmulti = -1;
			int blocksize = -1;
			String filename = null;

			switch (arg.length) {
			case 5:
				prefix_out = arg[4];
			case 4:
				prefix_in = arg[3];
			case 3:
				mapmulti = Integer.parseInt(arg[2]);
			case 2:
				blocksize = Integer.parseInt(arg[1]);
			case 1:
				filename = arg[0];
			default:
				filename = readString(
						"Filename (with no extention, but make sure the extention of your file is lowercase on *nix)",
						filename);
				blocksize = readInt("Size of a block", blocksize);
				mapmulti = readInt("Number of blocks to fit in a map", mapmulti);
				prefix_in = readString("Prefixed path for input file (enter to leave empty, include trailing /)",
						prefix_in);
				prefix_out = readString("Prefixed path for output files (enter to leave empty, include trailing /)",
						prefix_out);
			}

			File dir1 = new File(".");
			System.out.println("Current dir : " + dir1.getCanonicalPath() + " prefix_in: " + prefix_in
					+ " prefix_out: " + prefix_out);

			build(filename, prefix_in, prefix_out, blocksize, mapmulti);

		} catch (Exception e) {
			System.out.println("Error, stacktrace:");
			e.printStackTrace();
		}
	}

	public abstract void build(String filename, String pre_in, String pre_out, int blocksize, int mapmulti)
			throws IOException;

}
